<?php

namespace App\V3\Api\Controllers;

use App\V3\Api\Models\PackageContentModel;
use App\V3\Api\Models\PackageModel;
use App\V3\Api\Services\PackageService;
use App\Http\Controllers\Api\BaseController;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class PackageController extends BaseController
{
    /**
     * @套餐列表
     * created 在 2021/11/02 14:44 创建
     * @author SunWu
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function list(Request $request)
    {
        //参数验证
        $validator = Validator::make($data = $request->all(), [
            'pageSize' =>'required|integer',//页面大小
            'currentPage' =>'required|integer',//当前页
        ],[
            'pageSize.required' => '参数`pageSize`缺失',
            'pageSize.integer' => '参数`pageSize`仅能为数字',
            'currentPage.required' => '参数`currentPage`缺失',
            'currentPage.integer' => '参数`currentPage`仅能为数字',
        ]);

        //参数错误响应
        if ($info = $validator->errors()->first()) {
            return response()->json([
                'code' => 1,
                'msg' => $info,
                'data' => '',
            ]);
        }

        $keyword = empty($data['keyword'])?'':$data['keyword'];
        $data['main_id'] = $this->getMainUserId();
        if($keyword != '') {
            $result = PackageService::withSearch($keyword, $data);
        }
        else {
           
            $result = PackageService::withoutSearch($data);
        }

        return $this->success($result);
    }

    /**
     * 新建套餐或追加套餐内容
     * @param Request $request
     * AUTHOR  : Sun Wu
     * DateTime: 2022/3/18 14:05
     * @return \Illuminate\Http\JsonResponse
     */
    public function add(Request $request)
    {
        if($request->type) {
            $result = SELF::addPackage($request->all());
            $this->log('新建套餐', $request->all());
        }
        else {
            $result = SELF::packageContentAdd($request->all());
            $this->log('增加套餐内容', $request->all());
        }

        return $result;
    }

    //新建套餐
    private function addPackage($data)
    {
        //参数验证
        $validator = Validator::make($data, [
            'package_name' =>'required',//页面大小
        ],
            [
                'package_name.required' => '请填写套餐名'
            ]
        );

        //参数错误响应
        if ($info = $validator->errors()->first()) {
            return $this->fail('', $info);
        }

        $result = PackageService::add($data, $this->getMainUserId());

        if($result > 0) {
            return $this->success($result);
        }
        else {
            return $this->fail('', '套餐：'.$data['package_name'].'已存在');
        }
    }

    //新建套餐内容
    public function packageContentAdd($data)
    {
        //参数验证
        $validator = Validator::make($data, [
            'id' =>'required|integer',//套餐id
            'storage_manage_id' =>'required',//库存id
            'num' =>'required|integer|min:0',//套餐内容数量
        ],
            [
                'id.required' => '参数`套餐id`缺失',
                'id.integer' => '参数`套餐id`只能为数字',
                'storage_manage_id.integer' => '参数`库存id`只能为数字',
                'storage_manage_id.required' => '参数`库存id`缺失',
                'num.required' => '参数`套餐内物料数量`缺失',
                'num.integer' => '参数`套餐内物料数量`只能为数字',
                'num.min' => '参数`套餐内物料数量`必须大于或等于0',
            ]
        );

        //参数错误响应
        if ($info = $validator->errors()->first()) {
            return $this->fail('', $info);
        }

        $result = PackageService::packageContentAdd($this->getMainUserId(), $data);
        if($result > 0) {
            return $this->success($result);
        }
        else if($result < 0) {
            return $this->fail('', '库存内商品不存在');
        }
        else {
            return $this->fail('', '套餐内已存在相同商品');
        }
    }

    /**
     * 删除指定套餐或套餐内容
     * @param Request $request
     * AUTHOR  : Sun Wu
     * DateTime: 2022/3/18 16:35
     * @return mixed
     */
    public function del(Request $request)
    {
        $data = $request->all();

        if($data['type']) {
            $result = SELF::delPackage($data);
        }
        else {
            $result = SELF::packageContentDel($data);
        }

        return $result;
    }

    //删除套餐
    private function delPackage($data)
    {
        if(empty($data)) {
            return $this->fail('', '参数`套餐id`缺失');
        }

        PackageModel::where('id', $data['id'])->update(['delete_flag' => 1]);

        $this->log('删除套餐', $data);
        return $this->success();
    }

    //删除指定套餐内容
    private function packageContentDel($data)
    {
        //参数验证
        $validator = Validator::make($data, [
            'id' =>'required|integer',//套餐内容id
        ],
            [
                'id.required' => '参数`套餐内容id`缺失',
                'id.integer' => '参数`套餐内容id`只能为数字',
            ]
        );

        //参数错误响应
        if ($info = $validator->errors()->first()) {
            return $this->fail('', $info);
        }

        $packageId = PackageContentModel::where('id', $data['id'])->first();

        $status = PackageModel::where('id', $packageId['package_id'])->first();

        if(!$status['status']) {
            return $this->fail('', '该套餐已使用无法进行修改删除添加等操作');
        }

        PackageContentModel::where('id', $data['id'])->update(['delete_flag' => 1]);

        $this->log('删除套餐内容', $data);
        return $this->success();
    }

    /**
     * 套餐内容列表
     * @param Request $request
     * AUTHOR  : Sun Wu
     * DateTime: 2022/3/18 17:24
     * @return \Illuminate\Http\JsonResponse
     */
    public function contentList(Request $request)
    {
        //参数验证
        $validator = Validator::make($data = $request->all(), [
            'id' =>'required|integer',//套餐id
            'pageSize' =>'required|integer',//页面大小
            'currentPage' =>'required|integer',//当前页
        ],
            [
                'id.required' => '参数`套餐id`缺失',
                'id.integer' => '参数`套餐id`只能为数字',
                'pageSize.required' => '参数`pageSize`缺失',
                'pageSize.integer' => '参数`pageSize`只能为数字',
                'currentPage.required' => '参数`currentPage`缺失',
                'currentPage.integer' => '参数`currentPage`只能为数字',
            ]
        );

        //参数错误响应
        if ($info = $validator->errors()->first()) {
            return $this->fail('', $info);
        }

        return $this->success(PackageService::contentList($data));
    }

    /**
     * 修改套餐或套餐内容
     * @param Request $request
     * AUTHOR  : Sun Wu
     * DateTime: 2022/3/19 9:45
     * @return mixed
     */
    public function edit(Request $request)
    {
        $data = $request->all();

        if($data['type']) {
            unset($data['type']);
            $result = SELF::editPackage($data);
        }
        else {
            unset($data['type']);
            $result = SELF::packageContentEdit($data);
        }

        return $result;
    }

    //编辑套餐
    public function editPackage($data)
    {
        //参数验证
        $validator = Validator::make($data, [
            'id' =>'required|integer',//页面大小
            'package_name' =>'required',//当前页
        ],
            [
                'id.required' => '参数`套餐id`缺失',
                'id.integer' => '参数`套餐id`只能为数字',
                'package_name.required' => '参数`套餐名称`缺失',
            ]
        );

        //参数错误响应
        if ($info = $validator->errors()->first()) {
            return response()->json([
                'code' => 1,
                'msg' => $info,
                'data' => '',
            ]);
        }

        if(!SELF::checkPackageStatus($data['id'])) {
            return ['code' => 1, 'msg' => '该套操已使用无法进行修改删除添加等操作', 'data' => ''];
        }

        $result = PackageService::edit($data, $this->getMainUserId());

        switch($result) {
            case 0:
                return $this->fail('', '操作失败，请重试');
            case 1:
                $this->log('修改套餐', $data);
                return $this->success();
            case 2:
                return $this->fail('', '套餐'.$data['package_name'].'已存在');
        }
    }

    //编辑套餐内容
    public function packageContentEdit($data)
    {
        //参数验证
        $validator = Validator::make($data, [
            'id' =>'required|integer',//套餐内容id
            'storage_manage_id' =>'required|integer',//库存id
            'storage_id' =>'required|integer',//仓库id
            'storage_name' =>'required',//仓库名
            'storage_address' =>'required',//仓库地址
            'package_id' =>'required|integer',//套餐id
            'num' =>'required|integer|min:0',//套餐内容数量
        ],
            [
                'id.required' => '参数`套餐内容id`缺失',
                'id.integer' => '参数`套餐内容id`只能为数字',
                'package_id.required' => '参数`套餐id`缺失',
                'package_id.integer' => '参数`套餐id`只能为数字',
                'storage_manage_id.integer' => '参数`库存id`只能为数字',
                'storage_manage_id.required' => '参数`库存id`缺失',
                'storage_id.integer' => '参数`仓库id`只能为数字',
                'storage_id.required' => '参数`仓库id`缺失',
                'num.required' => '参数`套餐内物料数量`缺失',
                'num.integer' => '参数`套餐内物料数量`只能为数字',
                'num.min' => '参数`套餐内物料数量`必须大于或等于0',
                'storage_name.required' => '参数`仓库名`缺失',
                'storage_address.required' => '参数`仓库地址`缺失',
            ]
        );

        //参数错误响应
        if ($info = $validator->errors()->first()) {
            return $this->fail('', $info);
        }

        $packageInfo = PackageContentModel::where('id', $data['id'])->first();

        if(!SELF::checkPackageStatus($packageInfo['package_id'])) {
            return $this->fail('', '该套操已使用无法进行修改删除添加等操作');
        }

        $result = PackageService::packageContentEdit($data, $this->getMainUserId());

        if(!$result) {
            return $this->fail('', '修改失败，请稍后重试');
        }
        else {
            $this->log('修改套餐内容', $data);
            return $this->success();
        }
    }

    public function checkPackageStatus($packageId)
    {
        $checkPackageInfo = PackageModel::where('id', $packageId)->first();

        if(!$checkPackageInfo['status']) {
            return false;
        }
        else {
            return true;
        }
    }

    //指定套餐内容对应的在库信息列表
    public function packageStorageList(Request $request)
    {
        //参数验证
        $validator = Validator::make($data = $request->all(), [
            'id' =>'required|integer',//套餐内容id
        ],
            [
                'id.required' => '参数`套餐内容id`缺失',
                'id.integer' => '参数`套餐内容id`只能为数字',
            ]
        );

        //参数错误响应
        if ($info = $validator->errors()->first()) {
            return $this->fail('', $info);
        }

        return $this->success(
            PackageService::packageStorageList($this->getMainUserId(), $data)
        );
    }

    /**
     * 添加套餐内容-可用库存物料信息
     * AUTHOR  : Sun Wu
     * DateTime: 2022/3/19 15:38
     * @return \Illuminate\Http\JsonResponse
     */
    public function packageContentSelect(Request $request)
    {
        //参数验证
        $validator = Validator::make($data = $request->all(), [
            'id' =>'required|integer',//套餐id
        ],
            [
                'id.required' => '参数`套餐id`缺失',
                'id.integer' => '参数`套餐id`只能为数字',
            ]
        );

        //参数错误响应
        if ($info = $validator->errors()->first()) {
            return $this->fail('', $info);
        }

        return $this->success(PackageService::packageContentSelect($this->getMainUserId(), $data));
    }

    /**
     * 套餐出库
     * @param Request $request
     * AUTHOR  : Sun Wu
     * DateTime: 2022/3/19 15:45
     * @return \Illuminate\Http\JsonResponse
     */
    public function packageOut(Request $request)
    {
        //参数验证
        $validator = Validator::make($data = $request->all(), [
            'id' =>'required|integer',//套餐id
            'num' =>'required|integer',//套餐数量
        ],
            [
                'id.required' => '参数`套餐id`缺失',
                'id.integer' => '参数`套餐id`只能为数字',
            ]
        );

        //参数错误响应
        if ($info = $validator->errors()->first()) {
            return $this->fail('', $info);
        }

        $data['user_id'] = $this->getUserId();
        $data['main_id'] = $this->getMainUserId();

        return response()->json(
            PackageService::packageOut($data)
        );
    }
}
